package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/protobuf/types/known/emptypb"
	"mall/api/mall"
	"mall/internal/conf"
	"mall/internal/service"
	"mall/pkg/network/jsonpb"
	"mall/pkg/request_key"
	"mall/pkg/server"
	"os"
)

func main() {
	serverCtx, cancel := context.WithCancel(context.Background())
	defer func() {
		service.Server.Close(serverCtx)
		cancel()
		os.Exit(1)
	}()
	if err := server.InitService(serverCtx, conf.Conf, service.Server, false); err != nil {
		fmt.Println(err.Error())
		return
	}
	ctx := request_key.NewContextWithReqID(context.Background())
	conn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%s", conf.Conf.NetWorkConfig.RPCPort),
		grpc.WithInsecure(), grpc.WithBlock())
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	defer func() {
		_ = conn.Close()
	}()
	client := mall.NewMallClient(conn)
	resp, err := client.HealthCheck(ctx, &emptypb.Empty{})
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	m := jsonpb.Marshaler{}
	respStr, _ := m.MarshalToString(resp)
	fmt.Println(respStr)
}
